//
//  WDBusinessDetailController.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2017/1/11.
//  Copyright © 2017年 王笛. All rights reserved.
//

#import "WDBusinessDetailController.h"
#import "WDBusiness.h"
#import "XYBusinessDetailCell1.h"
#import "XYBusinessDetailItem.h"
#import "XYBaseItem.h"
#import "XYBusinessDetailFrame.h"
#import "XYBusinessRecordSelectVC.h"
#import "XYCallDetailVC.h"
#import "XYLocalAlertView.h"
#import "XYMapViewController.h"
@interface WDBusinessDetailController ()<UITableViewDataSource,UITableViewDelegate,XYLocalAlertViewDelegate>
{
    UITableView * _xyDetailTableView;
    UIView      * _xyHeaderView;
    UIView      * _xyTableHeaderView;
    int           _xyCurrentPage;
    int           _xyPageLimit;
    NSMutableArray * _xyDataMArr;
    UIButton   * _xyAddFollowBtn;
    UILabel    * _xyTitleLabel;
}
@end

@implementation WDBusinessDetailController

- (void)viewWillAppear:(BOOL)animated{
    [self xyReloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self xyInitData];
    [self loadNavItemWithLeftAction:@selector(clickNavLeftBtn) leftBtnImage:@"icon_back" leftBtnSelImage:@"icon_back" title:self.business.name rihgtAction:@selector(clickNavRightBtn) rightBtnImage:@"xy_icon_more" rightBtnSelImage:nil];
    [self xyLayoutHeaderView];
    [self xyLayoutTableView];
    [self xyLayoutAddFollowBtn];
    
    NSLog(@"传来的数据源:%@",self.business.id);
//    [self xyRequestBusinessDetailData];
}

- (void)clickNavLeftBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)clickNavRightBtn
{
}

- (void)xyInitData{
    _xyDataMArr = [[NSMutableArray alloc]init];

}

- (void)xyLayoutHeaderView{
    _xyHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,WDScreenW,xy(100))];
    [self.view addSubview:_xyHeaderView];
    _xyHeaderView.backgroundColor = WDNavSeleColor;

    [self xyLayoutCategoryView];
    
}

- (void)xyLayoutCategoryView{
    NSArray * statusArr = @[@"新建商户",@"潜在商户",@"成交商户",@"流失商户",@"长期商户"];
    NSArray * levelArr = @[@"A级",@"B级",@"C级"];
    NSString * level;
    if([self.business.level intValue] >0){
        level = levelArr[[self.business.level intValue] - 1];
    }else{
        level = @"-";
    }
    
    NSArray * label1Arr = @[@"联系人",@"状态",@"等级"];
    NSArray * label2Arr = @[[NSString stringWithFormat:@"%ld个",self.business.link_data.count],statusArr[[self.business.status intValue] -1],level];

    for(int i=0;i<3;i++){
        CGFloat xyW = WDScreenW/3;
        CGFloat xyH = CGRectGetHeight(_xyHeaderView.frame);
        CGFloat xyX = xyW * i;
        UIView * categoryView = [[UIView alloc]initWithFrame:CGRectMake(xyX, 0, xyW, xyH)];
        [_xyHeaderView addSubview:categoryView];
//        categoryView.backgroundColor = [UIColor greenColor];
        categoryView.tag = i;
        UITapGestureRecognizer * categoryTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(categoryTap:)];
        [categoryView addGestureRecognizer:categoryTap];
        
        UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, xyW, xy(20))];
        label1.center = CGPointMake(xyW/2, xyH/2-CGRectGetHeight(label1.frame)/2);
        [categoryView addSubview:label1];
        label1.text = label1Arr[i];
        label1.textAlignment = NSTextAlignmentCenter;
        label1.textColor = ColorForRGB(0xFFFFFF);
        label1.font = [UIFont systemFontOfSize:13];
        label1.alpha = 0.8;
        
        
        UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, xyW, xy(20))];
        label2.textAlignment = NSTextAlignmentCenter;
        label2.center = CGPointMake(xyW/2, xyH/2+CGRectGetHeight(label2.frame)/2);
        label2.font = [UIFont systemFontOfSize:14];

        [categoryView addSubview:label2];
        label2.text = label2Arr[i];
        label2.textColor = ColorForRGB(0xFFFFFF);
        
        
        if(i>1){
            return;
        }
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(xyW - xy(0.5), label1.frame.origin.y, xy(0.5), CGRectGetHeight(label1.frame) + CGRectGetHeight(label2.frame))];
        lineView.backgroundColor = [UIColor whiteColor];
        lineView.alpha = 0.5;
        [categoryView addSubview:lineView];
 
    }
}

- (UIView *)xyReturnTableHeaderView{
    UIView * subview = [[UIView alloc]initWithFrame:CGRectMake(0,0, WDScreenW, xy(30))];
    UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(xy(10), xy(8), xy(150), xy(14))];
    [subview addSubview:titleLabel];
    titleLabel.text = @"跟进记录";
    titleLabel.textColor = ColorForRGB(0x4c4c4c);
    titleLabel.font = [UIFont systemFontOfSize:14];
    _xyTitleLabel = titleLabel;
    return subview;
}

- (void)xyRefreshTitleLabel{
    
    if(_xyDataMArr.count == 1){
        XYBusinessDetailItem * item = _xyDataMArr[0];
        if([item.type isEqualToString:@"0"]){
            _xyTitleLabel.text = @"暂无跟进记录";

        }
    }
}

- (void)xyLayoutTableView{
    _xyTableHeaderView = [self xyReturnTableHeaderView];
    
    _xyDetailTableView =[[UITableView alloc]init];
    [self.view addSubview:_xyDetailTableView];
    _xyDetailTableView.tableHeaderView = _xyTableHeaderView;
    
    [_xyDetailTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(_xyHeaderView.mas_bottom);
        make.width.mas_equalTo(self.view);
        make.height.mas_equalTo(self.view.bounds.size.height - _xyHeaderView.bounds.size.height - xy(44) - xy(40));
    }];
    _xyDetailTableView.delegate = self;
    _xyDetailTableView.dataSource = self;
    _xyDetailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self xyLayoutRefreshTableView];
    [self xyLayoutLoadTableView];
}

- (NSArray *)xySubviewsFrame{
    
    if(_xySubviewsFrame == nil||_xySubviewsFrame.count != _xyDataMArr.count ||_xySubviewsFrame.count == 0){
        NSMutableArray * models = [NSMutableArray arrayWithCapacity:100];
        for(XYBusinessDetailItem * item in _xyDataMArr){
            XYBusinessDetailFrame * frame = [[XYBusinessDetailFrame alloc]init];
            [frame setXyDetailItem:item];
            [models addObject:frame];
        }
        _xySubviewsFrame = [models copy];
    }
    return _xySubviewsFrame;
}

- (void)xyLayoutAddFollowBtn{
    _xyAddFollowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_xyAddFollowBtn];

    CGFloat btnH = xy(40);
    [_xyAddFollowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(WDScreenW, btnH));
    }];
    _xyAddFollowBtn.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1.00];
    [_xyAddFollowBtn setTitle:@"添加跟进记录" forState:UIControlStateNormal];
    _xyAddFollowBtn.titleLabel.font = [UIFont systemFontOfSize:xy(11)];
    _xyAddFollowBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    [_xyAddFollowBtn setTitleColor:[UIColor colorWithRed:0.43 green:0.67 blue:0.90 alpha:1.00] forState:UIControlStateNormal];
    [_xyAddFollowBtn addTarget:self action:@selector(xyAddFollowBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _xyAddFollowBtn.layer.shadowColor = [UIColor colorWithRed:0.91 green:0.92 blue:0.92 alpha:1.00].CGColor;
    _xyAddFollowBtn.layer.shadowOffset =  CGSizeMake(1, 1);
    _xyAddFollowBtn.layer.borderWidth = 1;
    _xyAddFollowBtn.layer.borderColor = [UIColor colorWithRed:0.91 green:0.92 blue:0.92 alpha:1.00].CGColor;
    _xyAddFollowBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 300);
    
    CGFloat imgH = xy(15);
    UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(WDScreenW/2 - imgH*3,(btnH - imgH)/2, imgH, imgH)];
    [_xyAddFollowBtn addSubview:imgView];
    [imgView setImage:[UIImage imageNamed:@"xy_icon_addFollowBlue"]];

}

//代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _xyDataMArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellId = @"xyCell";
    XYBusinessDetailCell1 *  cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell){
       cell= [[XYBusinessDetailCell1 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
 
    }
    cell.xyLocalAlertView.delegate = self;
    cell.xyLocalAlertView.tag = indexPath.row;
    cell.xyDetailFrame = [self.xySubviewsFrame objectAtIndex:indexPath.row];
//        cell.backgroundColor = [UIColor grayColor];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    XYBusinessDetailFrame * xyDetailFrame = [self.xySubviewsFrame objectAtIndex:indexPath.row];

    return xyDetailFrame.xyCellH;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"didSelectRowAtIndexPath");
    
    XYCallDetailVC * callDetailVC = [[XYCallDetailVC alloc]init];
    XYBusinessDetailItem * detailItem = [_xyDataMArr objectAtIndex:indexPath.row];
    callDetailVC.xyFollow_id = detailItem.id;
    callDetailVC.xyBusinessDetailItem = detailItem;
    NSLog(@"惊讶:%@",callDetailVC.xyBusinessDetailItem.image_url);
    [self.navigationController pushViewController:callDetailVC animated:YES];
    
}

- (void)xyLocalAlertViewTap:(id)sender{
    XYLocalAlertView * localView = (XYLocalAlertView *)sender;
    XYBusinessDetailFrame * xyDetailFrame = [self.xySubviewsFrame objectAtIndex:localView.tag];
    
    XYMapViewController * xyMapVC = [[XYMapViewController alloc]init];
    CLLocationCoordinate2D coor;
    coor.latitude = [xyDetailFrame.xyDetailItem.latitude doubleValue];
    coor.longitude =[xyDetailFrame.xyDetailItem.longitude doubleValue];
    xyMapVC.xyLocalCoordinate  = coor ;
    
    [self.navigationController pushViewController:xyMapVC animated:YES];
}

//点击事件
- (void)categoryTap:(UITapGestureRecognizer *)sender{
    UIView * view = (UIView *)sender.view;
    NSLog(@"categoryTap:%ld",view.tag);
}

- (void)xyAddFollowBtnClick:(UIButton *)sender{
    NSLog(@"xyAddFollowBtnClick");
    XYBusinessRecordSelectVC * selectVC = [[XYBusinessRecordSelectVC alloc]init];
    selectVC.business = self.business;
    [self.navigationController pushViewController:selectVC animated:YES];
}

- (void)xyReloadData{
    _xyCurrentPage = 1;
    _xyPageLimit = 10;
    [_xyDataMArr removeAllObjects];
    _xySubviewsFrame = [NSArray array];
    [self  xyRequestBusinessDetailData];
}

- (void)xyLoadData{
    _xyCurrentPage +=1;
    _xyPageLimit = 10;
    [self  xyRequestBusinessDetailData];

}


//请求
- (void)xyRequestBusinessDetailData{
    
    NSDictionary * paraDic = @{@"supplier_id":self.business.id,@"current_page":[NSString stringWithFormat:@"%d",_xyCurrentPage],@"limit":[NSString stringWithFormat:@"%d",_xyPageLimit]};
    NSLog(@"参数:%@",paraDic);
    [WDHTTPMethod xy_requestPostJsonWithUrl:xyUrl_busineesDetail withItemClass:[XYBusinessDetailItem class] parameters:paraDic isAddCookie:YES success:^(id responseObject) {
        XYBaseItem * baseItem = responseObject;
        if([baseItem.error_code isEqualToString:@"0"]){
            [_xyDataMArr addObjectsFromArray:baseItem.data];
        }
        for(XYBusinessDetailItem * item in baseItem.data){
            NSLog(@"头像:%@,名字:%@",item.image_url,item.create_name);
        }
        [_xyDetailTableView reloadData];
        [self xyRefreshTitleLabel];
        NSLog(@"商户详情结果:%@",baseItem.data);
        [_xyDetailTableView.mj_header endRefreshing];
        [_xyDetailTableView.mj_footer  endRefreshing];
        
    } fail:^{
        [_xyDetailTableView.mj_header endRefreshing];
        [_xyDetailTableView.mj_footer  endRefreshing];
    }];
}

- (void)xyLayoutRefreshTableView{
    _xyDetailTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self xyReloadData];
    }];
    
   
}

- (void)xyLayoutLoadTableView{
    _xyDetailTableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        
        [self xyLoadData];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end












